package com.spring.demo4.factory;

import com.spring.demo4.factory.Joinpoint.MyMethodInvocation;
import com.spring.demo4.factory.advice.MyMethodBeforeAdvice;
import org.springframework.aop.BeforeAdvice;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * 1.MethodBeforeAdviceInterceptor:
 * 负责将 MethodBeforeAdvice 方法前置通知包装为MethodInterceptor 类型，创建这个类型的对象的时候需要传递一个 MethodBeforeAdvice 类型的参数
 * @author vhans
 */
@SuppressWarnings("serial")
public class MyMethodBeforeAdviceInterceptor implements MyMethodInterceptor, BeforeAdvice, Serializable {

    private final MyMethodBeforeAdvice advice;

    public MyMethodBeforeAdviceInterceptor(MyMethodBeforeAdvice advice) {
        Assert.notNull(advice, "Advice must not be null");
        this.advice = advice;
    }

    @Override
    public Object invoke(MyMethodInvocation mi) throws Throwable {
        // 负责调用前置通知的方法
        this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
        // 继续执行方法调用链
        return mi.proceed();
    }


}
